package mobi.greenplate.client.handler;

import mobi.greenplate.client.converter.FriendConverter;
import mobi.greenplate.client.domain.Friend;
import mobi.greenplate.client.domain.User;
import mobi.greenplate.client.converter.UserConverter;
import mobi.greenplate.client.parser.DomainHandler;

/**
 *
 * @author dmakariev
 */
public class FriendHandler extends DomainHandler<Friend> {

    private static final String FRIEND_ROOT = "//friend";
    private static final String FRIEND_TAG = "friend";
    private static final String OWNER_ROOT = "//friend/owner";
    private Friend friend = new Friend();
    private User owner = new User();

    @Override
    public Friend getEntity() {
        return friend;
    }

    @Override
    public void endTag(String parentPath, String tagName, String text) {
        if (parentPath.equalsIgnoreCase(FRIEND_ROOT)) {
            friend = FriendConverter.endTag(friend, tagName, text);
        }
        if (parentPath.equalsIgnoreCase(OWNER_ROOT)) {
            owner = UserConverter.endTag(owner, tagName, text);
        }
        if (tagName.equalsIgnoreCase(FRIEND_TAG)) {
            friend.setOwner(owner);
        }
    }

    @Override
    public void startTag(String parentPath, String tagName) {
        //do nothing
    }
}
